(function(window){
    function Game(option){
         this.ctx=option.ctx;
         this.imgArr=['birds','land','pipe1','pipe2','sky'];
         this.timer=null;//定时器
         this.roles=[];//所有的游戏对象放在数组中
         this.hero=null;

         this.startTime=new Date();
         this.endTime=0;
          this.Dvalue=0;
    }

    Game.prototype={
       constructor:Game,
        start:function(){ //游戏开始
            var that=this;
            this.loadImage(function(imgList){
                that.initGame(imgList);
                that.userControl();
                that.timer=setInterval(function(){
                    that.ctx.clearRect(0,0,that.ctx.canvas.width,that.ctx.canvas.height);
                    that.ctx.beginPath();
                    that.endTime=new Date();
                    that.Dvalue=that.endTime-that.startTime;
                    that.startTime=that.endTime;
                    that.render(that.Dvalue);
                    that.impact();
                },20)

            })
        },
        loadImage:function(callback){ //资源加载（图片）
            var imgList={};//存储所有图片
            var count=0;
            for (var i = 0; i < this.imgArr.length; i++) {
                var obj = this.imgArr[i];
                var img=new Image();
                imgList[obj]=img; //坑！！！
                img.onload=function(){
                    count++;
                    if(count>=5){
                        console.log(imgList);
                        callback&&callback(imgList);
                    }
                }
                img.src='imgs/'+obj+'.png';
            }
        },
        initGame:function(imgList){
            //天空
            for (var i = 0; i<3; i++) {
                var sky=new Game.Sky({
                    ctx:this.ctx,
                    img:imgList['sky'],
                    index:i
                })
                this.roles.push(sky);
            };

            for (var i = 0; i<6; i++) {
                var pipe=new Game.Pipe({
                    ctx:this.ctx,
                    upImg:imgList['pipe2'],
                    downImg:imgList['pipe1'],
                    index:i
                })
                this.roles.push(pipe);
            };

            for (var i = 0; i<4; i++) {
                var land=new Game.Land({
                    ctx:this.ctx,
                    img:imgList['land'],
                    index:i
                })
                this.roles.push(land);
            };

            var  bird=new Game.Bird({
                ctx:this.ctx,
                img:imgList['birds']
            });

            this.roles.push(bird);
            this.hero=bird;

            console.log(this.roles);

        },
        render:function(Dvalue){ //渲染
            for (var i = 0; i < this.roles.length; i++) {
                var obj = this.roles[i];
                obj.render(Dvalue);
            }
        },
        impact:function(){ //碰撞检测
            if(this.ctx.isPointInPath(this.hero.x,this.hero.y)||this.hero.y<0||this.hero.y>cs.height-112){
                // alert(11);
                this.over();
            }
        },
        userControl:function(){//用户控制
            var that=this;
            window.onclick=function(){
                that.hero.speed=-0.3;
            }
        },
        over:function(){ //游戏结束
            clearInterval(this.timer);
        }
    }


    window.Game=Game;
})(window);
